<?php
namespace  Ekadmin\Controller;

use \Think\Controller;

use Think\Page;



class TixianController extends CommonController

{

        public  function index()

        {




            $where['uniacid'] = $this->uniacid;

            $obj = M('tixian');



            $param = I('get.');

            if($param['status']){

                $where['status'] = $param['status'];

            }
            
            
            if($param['uid']){
                $where['uid'] = $param['uid'];
            }
            

            if($param['start'] && !$param['end']){
            
                $where['addtime'] = array('gt',strtotime($param['start']));
            
            }
            
            if(!$param['start'] && $param['end']){
            
                $where['addtime'] = array('lt',strtotime($param['end']));
            
            }
            
            if($param['start'] && $param['end']){
            
                $where['addtime'] = array('between',[strtotime($param['start']),strtotime($param['end'])]);
            
            }
            
            $count = $obj->where($where)->count();

            $page = new Page($count);
            
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
            
            if($list){
                for($i=0;$i<count($list);$i++){
                    $rs=M("user")->find($list[$i]["uid"]);
                    $list[$i]["name"]=$rs["name"];
                    $list[$i]["mobile"]=$rs["mobile"];
                    $list[$i]["head"]=$rs["head"];
                }
            }

            $this->assign('page',$page->show());
            
            $this->assign('count',$count);

            $this->assign('list',$list);

            
            $this->display();

        }
        
        /**
         *@批量同意打款 
         */
        public function isagree(){
            if(IS_POST){
            
                $param = I('id');
            
                if(is_array($param)){
            
                    $map['id'] = array('in',$param);
            
                }else{
            
                    $map['id'] = $param;
            
                }
                
                $map["uniacid"]=$this->uniacid;
                
                $rs=M('tixian')->where($map)->select();
                if($rs){
                    
                    $SSLCERT_PATH="./ThinkPHP/Library/Vendor/campanyPay/cert/apiclient_cert.pem";//支付证书
                    
                    $SSLKEY_PATH="./ThinkPHP/Library/Vendor/campanyPay/cert/apiclient_key.pem"; //支付key
                    
                    $conf=M("config")->where("uniacid=".$this->uniacid)->find();                                        //找出小程序相关信息
                    
                    Vendor('sendMoney');                                                        //引用提现类
                    
                    $wxpay = new \sendMoney($conf["appid"],$conf["mchid"],$conf["wxpaykey"],get_client_ip(),$SSLCERT_PATH,$SSLKEY_PATH);
                    //发送企业支付到零钱  开始
                    $sj=time();
                    
                    for($i=0;$i<count($rs);$i++){
                        
                        $desc="团长提现";
                        
                        $res=M("user")->where("id=".$rs[$i]["uid"])->find();
                        
                        $re = $wxpay->sendmoney($rs[$i]["jiner"], $res["utoken"],date("ymdHis").rand(1,9).rand(1,9).rand(1,9),$desc);
                        
                        
                        
                        if($re["return_code"]=="SUCCESS" && $re["result_code"]=="SUCCESS"){
                            
                            $brr=array(
                                "status"=>2,
                                "updatetime"=>$sj,
                                "reason"=>'管理员正常打款'
                            );
                            
                            M("tixian")->where("id=".$rs[$i]["id"])->setField($brr);
                            
//                             $crr=array(
//                                 "uid"   =>$rs[$i]["uid"],
//                                 "money"  =>$rs[$i]["jiner"],
//                                 "ntype"  =>2,
//                                 "addtime"=>$sj,
//                                 "info"   =>"团长申请提现",
//                             );
                            
//                             M("caiwu")->data($crr)->add();
                        }else{
                            
                            ajax_return(1,'提现失败');
                            
                        }
                    }
                    
                    ajax_return(0,'操作成功');
                }else{
                    
                    ajax_return(1,'操作失败，没有记录');
                    
                }

            }
            
            
        }
        
        
        /**
         *@批量拒绝打款 
         */
        public function isnotagree(){
            if(IS_POST){
            
                $param = I('id');
            
                if(is_array($param)){
            
                    $map['id'] = array('in',$param);
            
                }else{
            
                    $map['id'] = $param;
            
                }
                
                $map["uniacid"]=$this->uniacid;
            
            
                $rs=M('tixian')->where($map)->select();
                
                $sj=time();
                
                for($i=0;$i<count($rs);$i++){
                    
                    $kou_jiner=$rs[$i]["kou_jiner"];
                    
                    M("user")->where("id=".$rs[$i]["uid"])->setInc("brokerage",$kou_jiner);
                    
//                     $rt=M("user")->field("id,brokerage")->find($rs[$i]["uid"]);
                    
//                     $crr=array(
                        
//                         "uid"   =>$rs[$i]["uid"],
//                         "money"  =>$kou_jiner,
//                         "ntype"  =>1,
//                         "addtime"=>$sj,
//                         "info"   =>"提现拒绝退回",
//                         "balance"=>$rt["brokerage"]
//                     );
                    
//                     M("caiwu")->data($crr)->add();
                    
                    
                    /**
                     * @拒绝打款
                     * @array $brr
                     */
                    $brr=array(
                        "status"=>3,
                        "updatetime"=>$sj,
                        "reason"=>'提现异常，请联系客服咨询。'
                    );
                    
                    M("tixian")->where("id=".$rs[$i]["id"])->setField($brr);
                    
                }
                
                ajax_return(0,"批量拒绝打款操作成功");
                
            }    
            
        }
        
        

        /**
         *@批量删除记录 
         */
        public function del()

        {

            if(IS_POST){

                $param = I('id');

                if(is_array($param)){

                    $map['id'] = array('in',$param);

                }else{

                    $map['id'] = $param;

                }
                
                $map["uniacid"]=$this->uniacid;


                M('tixian')->where($map)->delete();

                ajax_return(0,'删除成功');

            }

        }
        
        /**
         *@单个同意打款
         */
        public function singleAgreePay(){
           if(IS_POST){
                
               $param=I("id");
                
                if(is_array($param)){
            
                    $map['id'] = array('in',$param);
            
                }else{
            
                    $map['id'] = $param;
            
                }
                $map['status'] = 1;
                
                $map["uniacid"]=$this->uniacid;
                
                $rs=M('tixian')->where($map)->select();
                if($rs){
                    
                    $SSLCERT_PATH="./ThinkPHP/Library/Vendor/campanyPay/cert/apiclient_cert.pem";//支付证书
                    
                    $SSLKEY_PATH="./ThinkPHP/Library/Vendor/campanyPay/cert/apiclient_key.pem"; //支付key
                    
                    $conf=M("config")->where("uniacid=".$this->uniacid)->find();                                                 //找出小程序相关信息
                    
                    Vendor('sendMoney');                                                        //引用提现类
                    
                    $wxpay = new \sendMoney($conf["appid"],$conf["mchid"],$conf["wxpaykey"],get_client_ip(),$SSLCERT_PATH,$SSLKEY_PATH);
                    //发送企业支付到零钱  开始
                    $sj=time();
                    
                    for($i=0;$i<count($rs);$i++){
                        
                        $desc="团长提现";
                        
                        $res=M("user")->where("id=".$rs[$i]["uid"])->find();
                        
                        $re = $wxpay->sendmoney($rs[$i]["jiner"], $res["utoken"],date("ymdHis").rand(1,9).rand(1,9).rand(1,9),$desc);
                        
                        
                        if($re["return_code"]=="SUCCESS" && $re["result_code"]=="SUCCESS"){
                            
                            $brr=array(
                                "status"=>2,
                                "updatetime"=>$sj,
                                "reason"=>'管理员正常打款'
                            );
                            
                            M("tixian")->where("id=".$rs[$i]["id"])->setField($brr);
                            
                        }else{
                            
                            ajax_return(1,'提现失败');
                            
                        }
                    }
                    
                    ajax_return(0,'操作成功');
                }else{
                    
                    ajax_return(1,'操作失败，没有记录');
                    
                }

            }
        }
        
        /**
         *@单个不同意打款 
         */
        public function singleNotAgreePay(){
          if(IS_POST){
            
                $param = I('id');
            
                if(is_array($param)){
            
                    $map['id'] = array('in',$param);
            
                }else{
            
                    $map['id'] = $param;
            
                }
            
                $map['status'] =1;
                
                $map["uniacid"]=$this->uniacid;
            
                $rs=M('tixian')->where($map)->select();
                
                $sj=time();
                
                for($i=0;$i<count($rs);$i++){
                    
                    $kou_jiner=$rs[$i]["kou_jiner"];
                    
                    M("user")->where("id=".$rs[$i]["uid"])->setInc("brokerage",$kou_jiner);

                    $brr=array(
                        "status"=>3,
                        "updatetime"=>$sj,
                        "reason"=>'提现异常，请联系客服咨询。'
                    );
                    
                    M("tixian")->where("id=".$rs[$i]["id"])->setField($brr);
                    
                }
                
                ajax_return(0,"拒绝打款操作成功");
                
            }
        }

}